package com.tigercel.xiangchengyun.ui.fragments

import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.SimpleItemAnimator
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.HotVideoAdapter
import com.tigercel.xiangchengyun.adapter.NewsAdapter
import com.tigercel.xiangchengyun.adapter.NewsBannerAdapter
import com.tigercel.xiangchengyun.entity.Banner
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.ui.custom.lbanners.LMBanners
import com.tigercel.xiangchengyun.ui.custom.lbanners.transformer.TransitionEffect
import com.tigercel.xiangchengyun.utils.ExpandGridView
import com.tigercel.xiangchengyun.utils.JsonUtil
import kotlinx.android.synthetic.main.fragment_video_normal.*
import org.jetbrains.anko.support.v4.toast
import java.text.SimpleDateFormat
import com.tigercel.xiangchengyun.utils.Global
class VideoParticularFragment : Fragment() {
    companion object {

        fun newInstance(channelPosition: Int): VideoParticularFragment? {
            val fragment = VideoParticularFragment()
            val bundle = Bundle()
            bundle.putInt("channelPosition", channelPosition)
            fragment.arguments = bundle
            return fragment

        }

    }

    private var containerView: View? = null
    private var banner: LMBanners<Any>? = null
    private var headBanner: View? = null
    private var emptyView: View? = null
    private var channelPosition: Int = 0
    private var VideoList: MutableList<HotVideoEntity>? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_video_normal, container, false)
        if (arguments != null) {
            channelPosition = arguments!!.getInt("channelPosition")
        }
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerview.layoutManager = LinearLayoutManager(activity)
        recyclerview.adapter = NewsAdapter(ArrayList()) {}
        headBanner = View.inflate(activity, R.layout.banners, null)
        emptyView = View.inflate(activity, R.layout.emptyview, null)
        banner = headBanner?.findViewById(R.id.banners)
        banner!!.setIndicatorBottomPadding(8)
        banner!!.setHoriZontalTransitionEffect(TransitionEffect.Default)
        recyclerview.refresh()
        getContent("create")


    }

    override fun onDestroy() {
        super.onDestroy()
//        banner!!.clearImageTimerTask()
    }

    private fun getContent(type: String) {
//        hotVideoList.clear()
//        tvvideoList.clear()
//        festivalvideoList.clear()
//        VideoList!!.clear()
        val devicesId = Settings.System.getString(activity!!.getContentResolver(), Settings.System.ANDROID_ID)
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        OkGo.get<String>(Global.baseUrl + "v1/channel/content")
                .tag(this)
                .params("channel_id", channelPosition)
                .params("device_code", devicesId)
                .params("user_id", "1")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val content = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        val data = JsonUtil.get_key_string("data", content!!)

                        if (status == "0") {
                            var banners1: MutableList<Banner>
                            banners1 = ArrayList()
                            banners1.clear()
                            val banners = JsonUtil.getList("banner", data)
                            if (banners.isNotEmpty()) {
                                if (type == "create") {
                                    recyclerview.addHeaderView(headBanner)
                                }
                                for (i in 0 until banners.size) {
                                    var img = JsonUtil.get_key_string("img", banners[i])
                                    var url = JsonUtil.get_key_string("url", banners[i])
                                    var name = JsonUtil.get_key_string("title", banners[i])
                                    var bannerBean = Banner(i, name, img, url)
                                    banners1.add(bannerBean)
                                }
                                banner!!.setAdapter(NewsBannerAdapter(activity), banners1 as List<Any>?)
                            }
                            val contentList = JsonUtil.getList("content", data)
                            Log.e("获取内容数量1", contentList.size.toString())
                            for (i in 0 until contentList.size) {
                                var contentView: View? = View.inflate(activity, R.layout.hot_video, null)
                                var name: TextView? = contentView!!.findViewById(R.id.video_title)
                                var grid: ExpandGridView? = contentView!!.findViewById(R.id.hot_video_recycleview)
                                VideoList = ArrayList()
                                var VideoAdapter: HotVideoAdapter? = null
                                val channel_id = JsonUtil.get_key_string("channel_id", contentList[i])
                                val name1 = JsonUtil.get_key_string("name", contentList[i])
                                val model = JsonUtil.get_key_string("model", contentList[i])
                                val prefix = JsonUtil.get_key_string("prefix", contentList[i])
                                val show_type = JsonUtil.get_key_string("show_type", contentList[i])
                                val realContent = JsonUtil.getList("content", contentList[i])
                                name!!.text = name1
                                for (k in 0 until realContent.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", realContent[k])
                                    var title = JsonUtil.get_key_string("title", realContent[k])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", realContent[k])
                                    var url = JsonUtil.get_key_string("url", realContent[k])
                                    var pub_time = JsonUtil.get_key_string("pub_time", realContent[k]).toLong()
                                    var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                    VideoList!!.add(hotVideoEntity)
                                }
                                VideoAdapter = HotVideoAdapter(activity!!.applicationContext, VideoList!!)
                                grid!!.adapter = VideoAdapter
                                recyclerview.addHeaderView(contentView)
                                grid.setOnItemClickListener { parent, view, position, id ->
                                    toast(VideoList!!.get(position).title)
                                }
                            }
                            (recyclerview.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
                            recyclerview.refreshComplete()
                            recyclerview.setLoadingListener(object : XRecyclerView.LoadingListener {
                                override fun onRefresh() {
                                    VideoList!!.clear()
                                    getContent("refresh")
                                }

                                override fun onLoadMore() {
//                                    getMore()
                                }
                            })
                        } else {
                            toast(getString(R.string.readFaile) + code)
                        }
                    }

                })
    }
}
